package com.hjj.springboot.controller.admin;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hjj.springboot.config.LogType;
import com.hjj.springboot.config.SystemLogs;
import com.hjj.springboot.entity.*;
import com.hjj.springboot.service.IAdminService;
import com.hjj.springboot.service.IRiderService;
import com.hjj.springboot.service.IWxUserInfoService;
import com.hjj.springboot.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/user")
public class AdminUserController {

    @Autowired
    private IWxUserInfoService wxUserInfoService;

    @Autowired
    private IAdminService adminService;

    @Autowired
    private IRiderService riderService;

//    根据条件分页查询微信用户信息
    @RequestMapping("/wxUserList")
    public R wxUserList(@RequestBody PageBean pageBean) {
        System.out.println(pageBean);
        String query=pageBean.getQuery().trim();
        Page<WxUserInfo> page=new Page<>(pageBean.getPageNum(),pageBean.getPageSize());
        Page<WxUserInfo> pageResult = wxUserInfoService.page(page,new QueryWrapper<WxUserInfo>().like(StringUtil.isNotEmpty(query),"nickName",query));
        Map<String,Object> map=new HashMap<>();
        map.put("total",pageResult.getTotal());
        map.put("userList",pageResult.getRecords());
        return R.ok(map);
    }


    //    根据条件分页查询管理员信息
    @RequestMapping("/adminList")
    public R adminList(@RequestBody PageBean pageBean) {
        String query=pageBean.getQuery().trim();
        Page<Admin> page=new Page<>(pageBean.getPageNum(),pageBean.getPageSize());
        Page<Admin> pageResult = adminService.page(page,new QueryWrapper<Admin>().like(StringUtil.isNotEmpty(query),"userName",query));
        Map<String,Object> map=new HashMap<>();
        map.put("total",pageResult.getTotal());
        map.put("userList",pageResult.getRecords());
        return R.ok(map);
    }

    //    根据条件分页查询骑手信息
    @RequestMapping("/riderList")
    public R riderList(@RequestBody PageBean pageBean) {
        String query=pageBean.getQuery().trim();
        Page<Rider> page=new Page<>(pageBean.getPageNum(),pageBean.getPageSize());
        Page<Rider> pageResult = riderService.page(page,new QueryWrapper<Rider>().like(StringUtil.isNotEmpty(query),"name",query));
        Map<String,Object> map=new HashMap<>();
        map.put("total",pageResult.getTotal());
        map.put("userList",pageResult.getRecords());
        return R.ok(map);
    }

    @RequestMapping("/wxUserNum")
    public R wxUserNum() {
        int num=wxUserInfoService.count();
        HashMap<String, Object> map = new HashMap<>();
        map.put("wxUserNum",num);
        return R.ok(map);
    }

    @SystemLogs(operation = "骑手审核状态",type = LogType.UPDATE)
    @GetMapping("/updateRiderStatus/{id}/status/{pass}")
    public R updateRiderStatus(@PathVariable(value = "id")Integer id,@PathVariable(value = "pass")Boolean pass) {
        Rider rider = riderService.getById(id);
        rider.setPass(pass);
        riderService.updateById(rider);
        return R.ok();
    }

    @GetMapping("/export")
    public void export(@RequestParam(required = false) String query, HttpServletResponse response)   throws Exception {
        String queryStr=query.trim();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("微信用户信息", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<WxUserInfo> wxUserInfoList = wxUserInfoService.list(new QueryWrapper<WxUserInfo>().like(StringUtil.isNotEmpty(queryStr),"nickName",query));
        EasyExcel.write(response.getOutputStream(),WxUserInfo.class)
                .sheet("微信用户信息")
                .doWrite(wxUserInfoList);
    }

    @GetMapping("/adminUser/export")
    public void exportAdminUser(@RequestParam(required = false) String query, HttpServletResponse response)   throws Exception {
        String queryStr=query.trim();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("管理员信息", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<Admin> adminList = adminService.list(new QueryWrapper<Admin>().like(StringUtil.isNotEmpty(queryStr),"userName",query));
        EasyExcel.write(response.getOutputStream(),Admin.class)
                .sheet("管理员信息")
                .doWrite(adminList);
    }

    @GetMapping("/riderUser/export")
    public void exportRiderUser(@RequestParam(required = false) String query, HttpServletResponse response)   throws Exception {
        String queryStr=query.trim();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("骑手信息", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<Rider> riderList = riderService.list(new QueryWrapper<Rider>().like(StringUtil.isNotEmpty(queryStr),"name",query));
        EasyExcel.write(response.getOutputStream(),Rider.class)
                .sheet("骑手信息")
                .doWrite(riderList);
    }

}
